package com.xunyoutest.shop.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import com.xunyoutest.shop.R;
import com.xunyoutest.shop.config.Api;
import com.xunyoutest.shop.ui.activity.MainActivity;
import com.xunyoutest.shop.ui.activity.WebActivity;
import com.xunyoutest.shop.utils.PreferencesUtils;

/**
 * @authorAdministrator 2020/9/21 0021
 * desc:
 */
public class AgreementDialog extends Dialog {
    private Activity mActivity;

    public AgreementDialog(@NonNull Activity context) {
        super(context, R.style.dialogstyle);
        mActivity = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_agreement);
        TextView contentTv = findViewById(R.id.dialog_agreement_content_tv);
        String agreement = getContext().getString(R.string.agreement);
        SpannableStringBuilder builder = new SpannableStringBuilder(agreement);
        ClickableSpan clickSpanPrivacy = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                //跳转隐私政策网址
                mActivity.startActivity(new Intent(getContext(), WebActivity.class)
                        .putExtra("url", Api.USER_AGREEMENT)
                );
            }
        };
        int userAgreementStart = agreement.indexOf("服务协议");

        builder.setSpan(clickSpanPrivacy, userAgreementStart-1, userAgreementStart + 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        ClickableSpan clickableSpanProtocol = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                //跳转服务协议
                mActivity.startActivity(new Intent(getContext(), WebActivity.class)
                        .putExtra("url", Api.HIDE_AGREEMENT)
                );
            }
        };
        int hideAgreementStart = agreement.indexOf("隐私权政策");
        builder.setSpan(clickableSpanProtocol, hideAgreementStart-1, hideAgreementStart + 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        contentTv.setText(builder);
        contentTv.setMovementMethod(LinkMovementMethod.getInstance());//调用此方法时文字点击事件才有效
        findViewById(R.id.dialog_agreement_not_use_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                mActivity.finish();
            }
        });
        findViewById(R.id.dialog_agreement_use_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                PreferencesUtils.put("isUseAgreement", true);
                mActivity.startActivity(new Intent(getContext(), MainActivity.class));
                mActivity.finish();
            }
        });
        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }
}
